﻿using System;
using System.Collections;
using System.Collections.Generic;
//using System.Data;
//using System.Data.Common;
using Petra.Gis.Geometry;
using Petra.Gis.DataServices;
using Petra.Gis.DataServices.Metadata;
//using System.Text;

namespace Petra.Gis.RenderingServices.PlanarBase 
{
  public class MapLegend : LeafLegendNode
  {

    protected MapBase _map;
    protected DisplayOrder _displayOrder;


    public MapLegend(MapBase map)
      : base(null, "Legend")
    {
      _map = map;
      _displayOrderList = new List<LegendNode>();
      _displayOrder = new DisplayOrder(this, _displayOrderList);
    }



    public MapBase Map
    {
      get { return _map; }
    }



    /// <summary>
    /// creates leaf node that contains no data but hold place for subNodes
    /// </summary>
    /// <param name="title"></param>
    /// <returns></returns>
    public LegendNode CreateLegendNode(string title)
    {
      return new LeafLegendNode(this, title);
    }




    public LegendNode CreateLegendNode(SpatialDbCommand dsc)
    {
      //create unique default name

      if (dsc.Metadata == null)
        throw new ArgumentException("no spatial metadata in SpatialDbCommand, cand append to legend");

      LegendNode tmpNode = new DataLegendNode(null, dsc, dsc.Metadata.Name);

      _displayOrderList.Insert(0, tmpNode);

      return tmpNode;
    }




    public LegendNode CreateLegendNode(SpatialDbCommand dsc, Renderer[] customRenderers)
    {
      LegendNode tmpNode = CreateLegendNode(dsc);

      tmpNode.Renderers.AddRange(customRenderers);

      _displayOrderList.Insert(0, tmpNode);

      return tmpNode;
    }



    public LegendNode CreateLegendNode(IEnumerable data, SpatialMetadata metadata)
    {

      if (metadata == null)
        throw new ArgumentException("null spatial metadata supplied, cand append to legend");

      foreach (object o in data)
        if (o.GetType() != typeof(GeometryBase))
          throw new ArgumentException(@"IEnumerable must return collection of GeometryBase in this case
                                       otherwise use CreateLegendNode with IConverter parameter");

      LegendNode tmpNode = new ObjectLegendNode(null, data, metadata, metadata.Name);

      _displayOrderList.Insert(0, tmpNode);

      return tmpNode;
    
    }



    public LegendNode CreateLegendNode(IEnumerable data, SpatialMetadata metadata, IConverter converter)
    {

      if (metadata == null)
        throw new ArgumentException("null spatial metadata supplied, cand append to legend");

      ////test if conversion vorks..
      //foreach (object o in data)
      //{
      //  converter.getGeometry(o);
      //  break;
      //}

      LegendNode tmpNode = new ObjectLegendNode(null, data, metadata, converter, metadata.Name);

      _displayOrderList.Insert(0, tmpNode);

      //tmpNode.renderers.AddRange(customRenderers);
      return tmpNode;

    }




    public DisplayOrder DisplayOrder
    {
      get { return _displayOrder; }
    }




    //public override MapLegend Legend
    //{
    //  get { return this; }
    //}


    //public abstract object DrawLegend();



  }
}
